package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.AttendanceType;
import com.ruoyi.system.mapper.AttendanceTypeMapper;
import com.ruoyi.system.service.IAttendanceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 考勤类别Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class AttendanceTypeServiceImpl implements IAttendanceTypeService {
    @Autowired
    private AttendanceTypeMapper attendanceTypeMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询考勤类别
     *
     * @param attendanceTypeId 考勤类别主键
     * @return 考勤类别
     */
    @Override
    public AttendanceType selectAttendanceTypeByAttendanceTypeId(Long attendanceTypeId) {
        return attendanceTypeMapper.selectAttendanceTypeByAttendanceTypeId(attendanceTypeId);
    }

    /**
     * 查询考勤类别列表
     *
     * @param attendanceType 考勤类别
     * @return 考勤类别
     */
    @Override
    public List<AttendanceType> selectAttendanceTypeList(AttendanceType attendanceType) {
        return attendanceTypeMapper.selectAttendanceTypeList(attendanceType);
    }

    /**
     * 新增考勤类别
     *
     * @param attendanceType 考勤类别
     * @return 结果
     */
    @Override
    public int insertAttendanceType(AttendanceType attendanceType) {
        attendanceType.setCreateTime(DateUtils.getNowDate());
        return attendanceTypeMapper.insertAttendanceType(attendanceType);
    }

    /**
     * 修改考勤类别
     *
     * @param attendanceType 考勤类别
     * @return 结果
     */
    @Override
    public int updateAttendanceType(AttendanceType attendanceType) {
        return attendanceTypeMapper.updateAttendanceType(attendanceType);
    }

    /**
     * 批量删除考勤类别
     *
     * @param attendanceTypeIds 需要删除的考勤类别主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceTypeByAttendanceTypeIds(Long[] attendanceTypeIds) {
        return attendanceTypeMapper.deleteAttendanceTypeByAttendanceTypeIds(attendanceTypeIds);
    }

    /**
     * 删除考勤类别信息
     *
     * @param attendanceTypeId 考勤类别主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceTypeByAttendanceTypeId(Long attendanceTypeId) {
        return attendanceTypeMapper.deleteAttendanceTypeByAttendanceTypeId(attendanceTypeId);
    }

    /**
     * 实体数据补全
     *
     * @param attendanceType 实体
     * @param userId         用户id
     * @param crateType      创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(AttendanceType attendanceType, Long userId, Integer crateType) {
        //主键
        if (attendanceType.getAttendanceTypeId() == null || "".equals(attendanceType.getAttendanceTypeId())) {
            attendanceType.setAttendanceTypeId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (attendanceType.getCreateBy() == null) {
                attendanceType.setCreateBy(userId.toString());
            }
            if (attendanceType.getCreateTime() == null) {
                attendanceType.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (attendanceType.getModifyBy() == null) {
            attendanceType.setModifyBy(userId);
        }
        //修改时间
        if (attendanceType.getModifyTime() == null) {
            attendanceType.setModifyTime(DateUtils.getNowDate());
        }
    }
}
